<template>
  <div class="competitiveModels">
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="90"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确 定</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  addCompeteModels,
  editCompeteModels
} from '@/api/potenCus/basedata/competitiveModels/index';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'brandModels',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log(this.rowData,"this.saveData")
      this.level = 1;
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.failModelId;
        this.dataSource = this.rowData.dataSource;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }else{
        this.formList.forEach(x => {
          x.initialValue = ''
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'failBrand',
          placeholder: '品牌',
          maxlength: 30,
          rules: [{ required: true, message: '品牌不能为空', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          initialValue: 10041001,
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null){
        let res = {};
        if (this.type === 'add') {
          // this.saveData={
          //   "failBrand": this.saveData.failBrand,
          //   "isValid": this.saveData.isValid
          // }
          res = await addCompeteModels({...this.saveData,level:this.level});
        }
        if (this.type === 'edit') {
          res = await editCompeteModels({...this.saveData, dataSource:this.dataSource, failModelId: this.recordId});
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success("保存成功!");
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
